package com.tro.mail.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tro.mail.dao.entity.MailForder;
import com.tro.mail.dao.entity.MailForderExample;
import com.tro.mail.dao.entity.MailInbox;
import com.tro.mail.dao.entity.MailMain;
import com.tro.mail.dao.mapper.MailForderMapper;
import com.tro.mail.dao.mapper.MailInboxMapper;
import com.tro.mail.dao.mapper.MailMainMapper;
import com.tro.mail.service.MailForderManager;
import com.tro.mail.util.MailStatus;

@Service
public class MailForderManagerImp implements MailForderManager {
	@Autowired
	MailForderMapper mapper;

	@Override
	public int countByExample(MailForderExample example) {

		return mapper.countByExample(example);
	}

	@Override
	public int deleteByExample(MailForderExample example) {

		return mapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(Long id) {

		return mapper.deleteByPrimaryKey(id);
	}

	@Autowired
	MailMainMapper mailMapper;
	@Autowired
	MailInboxMapper inBoxMapper;

	@Override
	public int insert(MailForder record) {
		if (record.getId() == null) {
			mapper.insert(record);
		}
		String ids = record.getIds();
		if (StringUtils.isNotBlank(ids)) {
			String[] strIds = ids.split(",");
			MailMain mail = new MailMain();
			MailInbox inBox = new MailInbox();
			int result = 0;
			for (String s : strIds) {
				// 更新id 对应数据
				mail = new MailMain();
				mail.setId(Long.parseLong(s));
				mail.setForderId(record.getId());
				mail.setMailStatu(MailStatus.MAIL_NORMAL);//只要移动了，修改状态
				result = mailMapper.updateByPrimaryKeySelective(mail);
				if (result == 0) {
					inBox = new MailInbox();
					inBox.setId(Long.parseLong(s));
					inBox.setForderId(record.getId());
					inBoxMapper.updateByPrimaryKeySelective(inBox);
				}
			}
		}

		return 1;
	}

	@Override
	public int insertSelective(MailForder record) {

		return mapper.insertSelective(record);
	}

	@Override
	public List<MailForder> selectByExample(MailForderExample example) {

		return mapper.selectByExample(example);
	}

	@Override
	public MailForder selectByPrimaryKey(Long id) {

		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByExampleSelective(MailForder record, MailForderExample example) {

		return mapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(MailForder record, MailForderExample example) {

		return mapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(MailForder record) {

		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(MailForder record) {

		return mapper.updateByPrimaryKey(record);
	}

}